﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMART_VIET_V01.Lib
{
    class dothiObject
    {
        public int toado_x { get; set; }
        public int toado_y { get; set; }
        public Color mausac { get; set; }
        public int type { get; set; }
        public int begin_x { get; set; }
        public int end_x { get; set; }
        public int begin_y { get; set; }
        public int end_y { get; set; }
        public Pen pen { get; set; }
        public Brush brush { get; set; }
        public Font font { get; set; }
        public int size { get; set; }
        public int k { get; set; }
        public int delay { get; set; }
        public Graphics g { get; set; }
        public Math_Function fn { get; set; }
        public bool stop { get; set; }
        public ArrayList arr { get; set; }

        public dothiObject() { }
        public void thuchienVe()
        {
            switch (type)
            {
                case 1:
                    veTrucDothi();
                    break;
                case 2:
                    veDothi();
                    break;
                case 3:
                    veObject();
                    break;
                case 4:
                    veChuthich();
                    break;
                default:
                    break;
            }
        }
        public void veTrucDothi()
        {
            //draw x
            g.DrawLine(pen, begin_x, toado_y, end_x, toado_y);
            //draw y
            g.DrawLine(pen, toado_x, begin_y, toado_x, end_y);
            //draw arrow x
            g.DrawLine(pen, end_x - 10, toado_y - 5, end_x, toado_y);
            g.DrawLine(pen, end_x - 10, toado_y + 5, end_x, toado_y);
            //draw arrow y
            g.DrawLine(pen, toado_x, 1, toado_x + 5, 10);
            g.DrawLine(pen, toado_x, 1, toado_x - 5, 10);

            //Pen pen2 = new Pen(Color.Blue, 2);

            //g.DrawLine(pen, PicPaint.Width / 2, 1, PicPaint.Width / 2, max_y);
            //g.DrawLine(pen2, (PicPaint.Width / 2) - (PicPaint.Height / 2), PicPaint.Height / 2, ((PicPaint.Width / 2) - (PicPaint.Height / 2)) + PicPaint.Height, PicPaint.Height / 2);

            // g.DrawLine()
            Font f = new Font("Tahoma", 10);
            Brush br = new SolidBrush(Color.Red);

            //g.DrawString("O", f, br, x0 - 15, y0);
            g.DrawString("x", f, br, end_x, toado_y - 20);
            g.DrawString("y", f, br, toado_x + 3, 1);

            int i;
            int num = 0;
            for (i = toado_x + k; i < end_x; i += k)
            {
                g.DrawLine(pen, i, toado_y - 3, i, toado_y + 2);
                num++;
                g.DrawString(num + "", f, br, i, toado_y);
            }
            num = 0;
            for (i = toado_x - k; i > begin_x; i -= k)
            {
                g.DrawLine(pen, i, toado_y - 3, i, toado_y + 2);
                num--;
                g.DrawString(num + "", f, br, i, toado_y);

            }
            num = 0;
            for (i = toado_y + k; i < end_x; i += k)
            {
                g.DrawLine(pen, toado_x - 3, i, toado_x + 2, i);
                num--;
                g.DrawString(num + "", f, br, toado_x - 20, i);
            }
            num = 0;
            for (i = toado_y - k; i > 0; i -= k)
            {
                g.DrawLine(pen, toado_x - 3, i, toado_x + 2, i);
                num++;
                g.DrawString(num + "", f, br, toado_x - 20, i);

            }
        }
        public void veDothi()
        {
            int min = -begin_x / k - 1;
            int max = begin_x / k + 1;

            double x, dx, fx1, fx2;
            int limit = end_y / 30 * k;
            Color cl = (mausac != null)? mausac : Color.Blue;
            Pen pen = new Pen(cl, 1);
            x = min;
            dx = 1.0f / k;

            fx1 = f(x, fn);
            int x1 = begin_x + (int)(x * k);
            int y1 = begin_y - (int)(fx1 * k);
            int x2,y2;


            if (stop) return;

            while (x < max)
            {
                x += dx;
                fx2 = f(x, fn);
                x2 = begin_x + (int)(x * k);
                y2 = begin_y - (int)(fx2 * k);

                try
                {
                    if (!(fx1 * fx2 < 0 && Math.Abs((int)(fx1 - fx2)) > k))
                    {
                        g.DrawLine(pen, x1, y1, x2, y2);
                        if (delay > 0) System.Threading.Thread.Sleep(delay);
                    }
                }
                catch { }

                x1 = x2;
                y1 = y2;
                fx1 = fx2;
            }
        }
       
        private double f(double x, Math_Function fn)
        {
            //return Math.Tan(x);

            Symbol sl;
            sl.m_type = SMART_VIET_V01.Lib.Type.Variable;
            sl.m_name = "x";
            sl.m_value = x;

            arr[0] = sl;
            fn.Variables = arr;
            fn.EvaluatePostfix();

            if (fn.Error)
            {
                MessageBox.Show(fn.ErrorDescription, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                stop = true;
            }
            return fn.Result;
        }
        public void veObject()
        {

        }
        public void veChuthich()
        {

        }
    }
}
